package com.moneyanalyzer.views.category;

import java.util.List;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;

import com.moneyanalyzer.R;
import com.moneyanalyzer.adapter.CategoryAdapter;
import com.moneyanalyzer.dao.CategoryDao;
import com.moneyanalyzer.model.Category;

public class FormCategory extends DialogFragment {
	
	private CategoryAdapter categoryAdapter = null;
	private Category category = null;

	public Category getCategory() {
		return category;
	}

	public void setCategory(Category category) {
		this.category = category;
	}

	public CategoryAdapter getCategoryAdapter() {
		return categoryAdapter;
	}

	public void setCategoryAdapter(CategoryAdapter categoryAdapter) {
		this.categoryAdapter = categoryAdapter;
	}

	/* (non-Javadoc)
	 * @see android.app.DialogFragment#onCreateDialog(android.os.Bundle)
	 */
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		LayoutInflater inflater = getActivity().getLayoutInflater();
		View rootView = inflater.inflate(R.layout.add_category_popup, null);
		final EditText categoryNameTextEdit = (EditText)rootView.findViewById(R.id.new_category);
		int idLabelBtnActionPositive = -1;
		if(this.category != null && this.category.getId() != 0){
			categoryNameTextEdit.setText(this.category.getName());
			idLabelBtnActionPositive = R.string.edit;
		}else{
			idLabelBtnActionPositive = R.string.add;
		}
		
		
		builder.setView(rootView).setPositiveButton(idLabelBtnActionPositive, new DialogInterface.OnClickListener() {
			
            @Override
            public void onClick(DialogInterface dialog, int id) {
                CategoryDao dao = CategoryDao.getInstance();
                
                if(categoryNameTextEdit.getText() != null){
                	if(category != null && category.getId() != 0){
                		category.setName(categoryNameTextEdit.getText().toString());
                		try {
							dao.update(category, getActivity().getApplicationContext());
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
                		
                	}else{
                		dao.insert(new Category(0, categoryNameTextEdit.getText().toString()), getActivity().getApplicationContext());
                	}
                	
                	List<Category> categorys = dao.list(getActivity().getApplicationContext());
                	if(categoryAdapter != null)
                		categoryAdapter.setMyList(categorys);
                }
                
            }
        })
        .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
            	FormCategory.this.getDialog().cancel();
            }
        });
		
		return builder.create();
	}

}
